package com.inspur.gs.sbssdl.sbwhgl.sbwhby.sbwhby.front.byszxz_frm.voactions;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bef.engine.entity.AssociationInfo;
import com.inspur.edp.bff.spi.*;
import com.inspur.edp.bef.api.action.*;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IValuesContainer;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import java.util.ArrayList;
import java.util.List;

public class bypzLinkVOAction extends AbstractFSAction<VoidActionResult> {

	private String[] ids;
	private String[] dataids;
	private String lx;
	public bypzLinkVOAction(String[] ids,String[] dataids,String lx) {

		this.ids = ids;
		this.dataids = dataids;
		this.lx = lx;
	}

	private static String sbtzCfg = "efd56b39-0e8d-4441-9fa1-2b019b6fd862";

	@Override
	public void execute() {
		SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
		try {
			//点检配置变更
			ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
			//设备台账的变更
			IStandardLcp lcp = iLcpFactory.createLcpByBEId(sbtzCfg);

			for (String id : ids) {
				if (lx.equals("1")) {
					ModifyChangeDetail curChange = new ModifyChangeDetail(id);
					curChange.getPropertyChanges().put("ryp",true);
					ArrayList<String> nodeCodes = new ArrayList();
					nodeCodes.add("bybz");
					ArrayList<String> hierarchyIds = new ArrayList();
					hierarchyIds.add(id);
					//子表变更
					for (String dataid : dataids) {
						IEntityData bymx = lcp.retrieveDefaultChild(nodeCodes, hierarchyIds);
						ModifyChangeDetail childChange = new ModifyChangeDetail(bymx.getID());
						AssociationInfo bypzInfo  = new AssociationInfo();
						bypzInfo.setValue("bybz",dataid);
						childChange.getPropertyChanges().put("bybz",bypzInfo);
						curChange.addChildChangeSet("bybz", childChange);
					}
					lcp.modify(curChange);
					lcp.save();
				} else if (lx.equals("2")) {
					ModifyChangeDetail curChange = new ModifyChangeDetail(id);
					curChange.getPropertyChanges().put("rep",true);
					ArrayList<String> nodeCodes = new ArrayList();
					nodeCodes.add("ebbz");
					ArrayList<String> hierarchyIds = new ArrayList();
					hierarchyIds.add(id);
					//子表变更
					for (String dataid : dataids) {
						hierarchyIds.add(id);
						IEntityData ebgldata = lcp.retrieveDefaultChild(nodeCodes, hierarchyIds);
						ModifyChangeDetail childChange = new ModifyChangeDetail(ebgldata.getID());
						AssociationInfo ebpzInfo  = new AssociationInfo();
						ebpzInfo.setValue("ebbz",dataid);
						childChange.getPropertyChanges().put("ebbz",ebpzInfo);
						curChange.addChildChangeSet("ebbz", childChange);
					}
					lcp.modify(curChange);
					lcp.save();
				}

			}
		} finally {
			SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
		}
	}
}